package com.monster.security.utils;

import com.monster.common.http.ResponseData;
import com.monster.json.JsonUtils;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.io.PrintWriter;

/**
 * 集成的一下统一工具封装
 *
 * @author kuangxz
 */
@Slf4j
public class SecurityUtils {
    /**
     * 写入返回字符串
     *
     * @param response 响应对象
     * @param code     响应的code
     * @param message  响应的消息
     * @param success  成功失败
     */
    public static void writerResponseData(HttpServletResponse response, String code, Object object, String message, boolean success) {
        response.setContentType("application/json;charset=utf-8");
        response.setStatus(200);
        try (PrintWriter out = response.getWriter()) {
            out.write(JsonUtils.toJson(success ? ResponseData.success(object, message) : ResponseData.error(String.valueOf(code), message)));
            out.flush();
        } catch (IOException e) {
            log.error("写入返回数据失败！", e);
        }
    }
}
